Spring Cloud Feign হল একটি declarative HTTP client, যা HTTP API কল করার জন্য খুবই সুবিধাজনক। Feign আপনাকে HTTP ক্লায়েন্ট কোড কম্পাইল করতে সাহায্য করে এবং বিভিন্ন HTTP সার্ভিসের সাথে যোগাযোগ করতে সহজ পদ্ধতি প্রদান করে। Feign এর মাধ্যমে আপনি কাস্টম কনফিগারেশন এবং Error Handling করতে পারেন, যা মাইক্রোসার্ভিসগুলোর মধ্যে নিরাপদ এবং কার্যকরী যোগাযোগের জন্য সহায়ক।
১. Feign Client Configuration: Custom Configuration
Feign Client-এর জন্য কাস্টম কনফিগারেশন তৈরি করতে হলে, আপনাকে Feign Client এর জন্য কনফিগারেশন ক্লাস তৈরি করতে হবে। এই ক্লাসের মধ্যে আপনি বিভিন্ন কাস্টম timeout, interceptors, এবং error handling কনফিগারেশন করতে পারেন।
Feign Client Custom Configuration:
import feign.Logger;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import feign.Feign;
import feign.Request;
import feign.codec.ErrorDecoder;
@Configuration
public class FeignConfig {
// Customize Feign Client timeout
@Bean
public Request.Options options() {
return new Request.Options(5000, 10000); // 5 seconds connection timeout, 10 seconds read timeout
}
// Customize error decoder
@Bean
public ErrorDecoder errorDecoder() {
return new MyErrorDecoder();
}
// Customize Feign logging level
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL; // FULL logging for debugging purposes
}
// Customize Feign client with interceptors
@Bean
public ClientHttpRequestInterceptor interceptor() {
return (request, body, execution) -> {
// Add custom header or any request modification here
request.getHeaders().add("Authorization", "Bearer my-token");
return execution.execute(request, body);
};
}
@Bean
public Feign.Builder feignBuilder() {
return Feign.builder().requestInterceptor(interceptor());
}
}
options(): Feign এর connection এবং read timeout কাস্টমাইজ করা যায়।errorDecoder(): কাস্টম error handling ব্যবহার করা হয়।feignLoggerLevel(): Feign ক্লায়েন্টের লগিং লেভেল সেট করা হয় (e.g.,NONE,BASIC,HEADERS,FULL).interceptor(): HTTP রিকোয়েস্টের আগে কাস্টম হেডার বা কোনো অতিরিক্ত মডিফিকেশন করতেRequestInterceptorব্যবহার করা হয়।
২. Feign Client তৈরি করা:
Feign Client তৈরি করতে হলে, একটি Interface তৈরি করতে হয় যা HTTP API কলগুলি সরাসরি রেফারেন্স করে।
Feign Client Interface:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "user-service", url = "http://localhost:8080", configuration = FeignConfig.class)
public interface UserServiceClient {
@GetMapping("/users/{id}")
String getUserById(@PathVariable("id") Long id);
}
- এখানে,
configuration = FeignConfig.classব্যবহার করা হয়েছে যা কাস্টম কনফিগারেশন লোড করতে সহায়ক।
৩. Error Handling with Feign
Feign এ Error Handling এর জন্য ErrorDecoder ব্যবহার করা হয়। আপনি কাস্টম ErrorDecoder তৈরি করতে পারেন যা Feign সার্ভিস কলের ত্রুটিগুলি হ্যান্ডল করবে।
ErrorDecoder ব্যবহার করা:
import feign.Response;
import feign.codec.ErrorDecoder;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
public class MyErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() == HttpStatus.NOT_FOUND.value()) {
// Example: handle 404 error
return new ResourceNotFoundException("Resource not found: " + methodKey);
} else if (response.status() == HttpStatus.BAD_REQUEST.value()) {
// Example: handle 400 error
return new BadRequestException("Bad Request: " + methodKey);
}
return new Exception("Generic error: " + response.status());
}
}
decode(): এই মেথডে, HTTP রেসপন্স কোডের উপর ভিত্তি করে কাস্টম Exception তৈরি করা হয়। উদাহরণস্বরূপ, যদি404 Not Foundরেসপন্স আসে, তবেResourceNotFoundExceptionথ্রো হবে।
Custom Exceptions:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException {
public BadRequestException(String message) {
super(message);
}
}
@ResponseStatus: এই অ্যানোটেশনটি ব্যবহার করে আপনি কাস্টম HTTP Status যুক্ত করতে পারেন, যাতে ক্লায়েন্ট সঠিক রেসপন্স পায়।
৪. Feign Client-এ Error Handling ব্যবহার:
Feign Client এর মধ্যে ErrorDecoder এর মাধ্যমে ত্রুটি হ্যান্ডলিং করা যায়। এবার, আমরা আমাদের কাস্টম ErrorDecoder ব্যবহার করে Client এর রেসপন্স হ্যান্ডল করবো।
Feign Client Example with Error Handling:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
private final UserServiceClient userServiceClient;
@Autowired
public UserController(UserServiceClient userServiceClient) {
this.userServiceClient = userServiceClient;
}
@GetMapping("/getUser/{id}")
public String getUser(@PathVariable("id") Long id) {
try {
return userServiceClient.getUserById(id);
} catch (Exception e) {
return "Error occurred: " + e.getMessage();
}
}
}
এখানে UserServiceClient এর মাধ্যমে API কল করা হচ্ছে এবং যদি কোনো ত্রুটি ঘটে, তবে কাস্টম Exception এর মেসেজ হ্যান্ডল করা হবে।
৫. Feign Error Handling এর জন্য Best Practices:
- Custom ErrorDecoder ব্যবহার করুন: প্রতিটি HTTP স্ট্যাটাস কোডের জন্য কাস্টম Exception তৈরি করুন।
- Fallback Method: Feign এর Hystrix বা Resilience4J এর সাথে fallback মেথড ব্যবহার করুন যাতে সার্ভিস ডাউন হলে fallback রেসপন্স প্রদান করা যায়।
- Logging: Feign Client এর মাধ্যমে API কলের লগ রাখতে Logger.Level ব্যবহার করুন (যেমন
Logger.Level.FULL). - Timeout: Feign Client কনফিগারেশনে টাইমআউট সময় ঠিকভাবে কনফিগার করুন।
- Error Message Propagation: কাস্টম ত্রুটি মেসেজের মাধ্যমে, ক্লায়েন্ট এবং সার্ভিসের মধ্যে উপযুক্ত ত্রুটি মেসেজ প্রেরণ নিশ্চিত করুন।
উপসংহার:
Feign একটি খুবই শক্তিশালী HTTP ক্লায়েন্ট যা সহজেই মাইক্রোসার্ভিসগুলির মধ্যে যোগাযোগ স্থাপন করতে সাহায্য করে। তবে, সঠিক custom configuration এবং error handling ছাড়া কোনো সিস্টেমের স্থিতিশীলতা বজায় রাখা কঠিন হতে পারে। ErrorDecoder ব্যবহার করে কাস্টম ত্রুটি হ্যান্ডলিং, কনফিগারেশন টিউনিং, এবং fallback প্যাটার্ন প্রয়োগ করে আপনি আপনার অ্যাপ্লিকেশনকে আরো নিরাপদ এবং কার্যকরী করতে পারেন।
যদি আরো কোনো বিষয় বা কনফিগারেশন নিয়ে সাহায্য দরকার হয়, জানাবেন! 😊
Read more